#include <QPainter>

#include "UIDialogView.h"
#include "UIFont.h"

// ===========================================================================
//  PRIVATE Class
// ===========================================================================
class UIDialogViewPrivate {
    public:
        QString title;
};

// ===========================================================================
//  PUBLIC Constructors/Destructor
// ===========================================================================
UIDialogView::UIDialogView (QGraphicsItem *parent)
    : UIControl(parent), d(new UIDialogViewPrivate)
{
}

UIDialogView::~UIDialogView() {
    delete d;
}

// ===========================================================================
//  PUBLIC Properties
// ===========================================================================
QString UIDialogView::title (void) const {
    return(d->title);
}

void UIDialogView::setTitle (const QString& title) {
    d->title = title;
}

// ===========================================================================
//  PROTECTED Methods
// ===========================================================================
void UIDialogView::drawRect (QPainter *painter, const QRect& rect) {
    int h = rect.height();
    int w = rect.width();
    int x = rect.x();
    int y = rect.y();

    QLinearGradient gradient(x, y, x, y + h);
    gradient.setColorAt(0.0f, QColor(0x8f, 0x97, 0xa2));
    gradient.setColorAt(0.14f, QColor(0x4b, 0x5d, 0x76));
    gradient.setColorAt(0.14001f, QColor(0x2e, 0x43, 0x60));
    gradient.setColorAt(0.43f, QColor(0x2e, 0x43, 0x60));
    gradient.setColorAt(1.0f, QColor(0x42, 0x57, 0x76));

    painter->setBrush(gradient);
    painter->setPen(QColor(0xa6, 0xac, 0xb3));
    painter->drawRoundedRect(x, y, w, h, 10, 10);

    int ellipseSize = 24;
    int ellipsePad = 8;
    QRectF ellipseRect(w - ellipseSize - ellipsePad, ellipsePad, ellipseSize, ellipseSize);
    painter->setPen(QPen(Qt::white, 2));
    painter->setBrush(Qt::transparent);
    painter->setFont(UIFontSmallBold(painter->font()));
    painter->drawEllipse(ellipseRect);
    painter->drawText(ellipseRect, Qt::AlignCenter, "X");

    painter->setPen(Qt::white);
    painter->setFont(UIFontNormalBold(painter->font()));
    painter->drawText(x, y, w, qRound(h * 0.19f), 
                      Qt::AlignHCenter | Qt::AlignBottom, d->title);
}

void UIDialogView::singleTapEvent (UITouch *touch) {
    QPointF localPos = mapFromScene(touch->currentPos());
    if (QRectF(width() - 100, 0, 100, 100).contains(localPos))
        emit closeTouched();
}

